package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.dao.DeviceBaseMapper;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.dao.SysUserHospitalMapper;
import net.zigin.nhi.could.waste.dto.DeviceClassifyDto;
import net.zigin.nhi.could.waste.vo.DeviceClassifyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.DeviceClassifyMapper;
import net.zigin.nhi.could.waste.model.DeviceClassify;
import net.zigin.nhi.could.waste.service.DeviceClassifyService;

import java.util.*;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-12 17:47:15                                               
##################################################################*/


/**
 * DeviceClassify服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: DeviceClassifyServiceImpl
 * @date 2021-9-12 17:47:15
 */
@Service
public class DeviceClassifyServiceImpl extends AbstractServiceImpl<DeviceClassify, String> implements DeviceClassifyService {
    @Autowired
    private DeviceClassifyMapper deviceClassifyMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;
    @Autowired
    private DeviceBaseMapper deviceBaseMapper;

    @Override
    public BaseMapper<DeviceClassify, String> getMapper() {
        return deviceClassifyMapper;
    }

    @Override
    public String saveOrUpdate(DeviceClassifyDto deviceClassifyDto) {
        AssertUtil.isNull(deviceClassifyDto, "");
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        if (StringUtil.isNullOrEmpty(deviceClassifyDto.getId())) {
            AssertUtil.isNull(deviceClassifyDto.getName(), "");
            AssertUtil.isNull(deviceClassifyDto.getCode(), "");
            deviceClassifyDto.setId(StringUtil.get32UUID());
            deviceClassifyDto.setCreateTime(new Date());
            deviceClassifyDto.setModifyTime(new Date());
            deviceClassifyDto.setCreateUser(tokenInfo.getLoginName());
            deviceClassifyDto.setHospitalBaseId(sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId()));
            DeviceClassify deviceClassify = ReflectUtil.copyProperties(deviceClassifyDto, DeviceClassify.class);
            deviceClassifyMapper.insert(deviceClassify);
            return deviceClassify.getId();
        } else {
            deviceClassifyDto.setModifyTime(new Date());
            deviceClassifyDto.setModifyUser(tokenInfo.getLoginName());
            DeviceClassify deviceClassify = ReflectUtil.copyProperties(deviceClassifyDto, DeviceClassify.class);
            deviceClassifyMapper.updateByPrimaryKeySelective(deviceClassify);
            return deviceClassify.getId();
        }
    }

    @Override
    public List<DeviceClassifyVo> getList(DeviceClassifyDto deviceClassifyDto) {
        Map<String, Object> paramMap = getDeviceClassifyParam(deviceClassifyDto);
        List<DeviceClassify> deviceClassifies;
        if (deviceClassifyDto.getIsPage()) {
            deviceClassifies = deviceClassifyMapper.selectListByPage(paramMap);
        } else {
            deviceClassifies = deviceClassifyMapper.selectListByPage(paramMap);
        }
        if (deviceClassifies.isEmpty()) {
            return new ArrayList<>();
        }
        return ReflectUtil.copyProperties(deviceClassifies, DeviceClassifyVo.class);
    }

    @Override
    public Integer getCount(DeviceClassifyDto deviceClassifyDto) {
        Map<String, Object> paramMap = getDeviceClassifyParam(deviceClassifyDto);
        return deviceClassifyMapper.selectCount(paramMap);
    }

    @Override
    public Boolean judgeRemove(String id) {
        int count = deviceBaseMapper.selectCount(ParamsMapUtil.getInstance().addParam("deviceClassifyId", id));
        return count == 0;
    }

    private Map<String, Object> getDeviceClassifyParam(DeviceClassifyDto deviceClassifyDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        deviceClassifyDto.setHospitalBaseId(sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId()));
        return ParamsMapUtil.getInstance()
                .addParam("hospitalBaseId", deviceClassifyDto.getHospitalBaseId())
                .addParam("name", deviceClassifyDto.getName())
                .addParam("code", deviceClassifyDto.getCode())
                .addParam("pageSize", deviceClassifyDto.getPageSize())
                .addParam("pageIndex", deviceClassifyDto.getPageIndex());
    }
}